/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package saphre.demo;

import java.util.Arrays;
import saphre.commands.CmdGappy;
import saphre.commands.CmdGappyParameters;
import saphre.commands.CmdRepeats;
import saphre.commands.CmdRepeatsParameters;
import saphre.core.SaphreBase;
import saphre.core.Store;
import saphre.core.SuffixArray;
import static saphre.core.SuffixArray.store;
import util.Interval;
import util.statistics.StatisticsHandler;

/**
 *
 * @author Niko Schenk
 */
public class CorpusDemo {

    /**
     * Demo client.
     *
     * @param args
     */
    public static void main(String[] args) {

        // Project path.
        String projectPath = "corpora/";
        // Corpus name.
        String corpusName = "mining_engineering";

        // Corpus data.
        String corpusPath = projectPath + corpusName + "/corpus/";

        // Set up SaphreBase object with pointers to regex, normalizer,
        // and boundary symbols.
        SaphreBase saphreBase = new SaphreBase(null,
                corpusPath,
                projectPath + corpusName + "/loadOptions/regex",
                projectPath + corpusName + "/loadOptions/norm",
                projectPath + corpusName + "/loadOptions/boundary",
                true, null);

        // Generate suffix array and components.
        // Serialize data files.
        saphreBase.invoke();

        // Run the commands, i.e. find repeats and gappy phrases.
        runRepeatsCommand(saphreBase);
        runGappyCommand(saphreBase);
        
        accessDataStructures(saphreBase);
    }

    /*
     * Find repeats in the corpus.
     */
    private static void runRepeatsCommand(SaphreBase saphreBase) {
        // Repeated n-grams.
        System.out.println("Extracting repeats...");
        // Set parameter values.
        CmdRepeatsParameters repeatsParameters = new CmdRepeatsParameters();
        // Search for all n-grams containing the word "e".
        repeatsParameters.setContainsTerm("e");
        repeatsParameters.setWriteToFile(false);
        // Execute repeats command.
        CmdRepeats cmdRepeatsCommand = new CmdRepeats();
        cmdRepeatsCommand.extractRepeats(saphreBase, repeatsParameters);
        System.out.println("... done.\n\n");
    }

    /*
     * Extract discontinuous (gappy) phrases from the corpus.
     */
    private static void runGappyCommand(SaphreBase saphreBase) {
        // Gappy phrases.
        // Set parameters and values.
        System.out.println("Extracting gappy phrases...");
        CmdGappyParameters gappyParameters = new CmdGappyParameters();
        gappyParameters.setMinNumberOfFillers(1);
        gappyParameters.setMinRatio(0);
        gappyParameters.setMaxTfOfRightPartInCorpus(100);
        gappyParameters.setMaxTfOfLeftPartInCorpus(100);
        gappyParameters.setWriteToFile(false);
        // Execute gappy phrases command.
        CmdGappy cmdGappyCommand = new CmdGappy();
        cmdGappyCommand.executeCommandGappy(saphreBase, gappyParameters);
        System.out.println("... done.\n\n");
    }

    /*
     * Illustrates how to access the suffix array compontents,
     * i.e. internal data structures.
     */
    private static void accessDataStructures(SaphreBase saphreBase) {

        // Illustrates how to access the data structures.
        Store myStore = saphreBase.getStore();
        SuffixArray sa = saphreBase.getSa();
        SuffixArray pa = sa.prefixArray();

        int numTokens = store.numTokens();
        System.out.println("Number of tokens in the corpus: " + numTokens);
        double numDocs = (double) store.numDocs() - 2; // Number of documents minus 2 sentinels.
        System.out.println("Number of documents: " + numDocs);
        
        

        // Search for a word.
        String ngram = "i";
        int[] arr = new int[1];
        arr[0] = myStore.toInt(ngram); 

        // Get the interval in the linearised suffix tree.
        Interval resultingInterval = sa.search(arr);
        int lcp = sa.getLcp(resultingInterval); // longest-common prefix value.
        int tf = resultingInterval.tf(); // term-frequency of interval (repeat).
        int df = resultingInterval.df(sa, myStore); // document frequency of repeat.
        double ridf = StatisticsHandler.ridf(tf, df, numDocs); // Ridf.
        double mi = StatisticsHandler.miGeneralNgram(arr, sa); // Mutual information.

        // Check if interval (repat) is maximal and/or supermaximal.
        boolean isMaximal = SuffixArray.isMaximal(arr, sa.top, sa, pa, store);
        boolean isSupermaximal = StatisticsHandler.isSupermaximal(resultingInterval, sa, lcp);

        System.out.println("Ngram: " + ngram);
        System.out.println("Interval: [ "
                + resultingInterval.lb() + "-" + resultingInterval.rb() + " ]");

        System.out.println("Term frequency: " + tf);
        System.out.println("Document frequency: " + df);
        System.out.println("Ridf: " + ridf);
        System.out.println("Mutinfo: " + mi);

        System.out.println("Maximal: " + isMaximal);
        System.out.println("Supermaximal: " + isSupermaximal);
    }
}
